MyMap = {}
/**
	 *在地图上显示指定车辆的GPS数据
	 */
MyMap.showVehicleOnMap = function(vehicleId)
	{
		//点击车辆树中的车辆节点，或者点击实时数据表格中的数据行，调用本方法，然后添加魔板显示信息
		// console.log("点击实时数据行，开始获取车辆的实时数据.............author:shinion.......");
		//调用ajax方法获得车辆的实时数据
		var url = globalConfig.webPath + '/realData/refresh.action';
		var me = this;
	    $.getJSON(url, {vehicleId:vehicleId}, function(result)
		 {
			 if(result.success == false )
			 return;
			 var rd = result.data;//GPS实时数据
			 //如果驾驶员不存在，则获取
			 if(typeof(rd.driverName)!= "undefined" && rd.driverName.length <= 0){
				var url2 = globalConfig.webPath + '/driverInfo/getLastInCardDriver.action';
				var param = {};
				param.vehicleId = vehicleId;
				Utility.getAjaxData(url2,JSON.stringify(param),function(data2){
				//    console.log("接收到数据最后的插卡记录信息.........");
				//    console.log(data2);
				   if(data2.success){
					   if(data2.data.length > 0){
						   rd.driverName = data2.data[0].driverName;
					   }
				   }
				//    console.log(rd);
				   show(rd);
				});
			 }else{
				 //如果驾驶员信息存在，则不操作
				 show(rd);
			 }
			 
			 

			function show(rd) {
				//mytree.fillGrid(rd);//填充到车辆树底部的车辆属性表格中
				var vehicleInfo = me.convertRecord(rd);
				var mapHandler = me.getMainMapHandler();
				if (mapHandler == null)
					return;
				mapHandler.centerMarkerOverlays(vehicleInfo, true, true);
			}

			 
							
		});
	}


//author：shinion
//date：2019-01-02
//设置显示车辆到地图上
MyMap.showOneVehicleOnMap = function (vehicleId) {
	//调用ajax方法获得车辆的实时数据
	var url = globalConfig.webPath + '/realData/refresh.action';
	var me = this;
	$.getJSON(url, { vehicleId: vehicleId }, function (result) {
		if (result.success == false)
			return;
		var rd = result.data;//GPS实时数据
		//mytree.fillGrid(rd);//填充到车辆树底部的车辆属性表格中
		var vehicleInfo = me.convertRecord(rd);
		var mapHandler = me.getMainMapHandler2();
		if (mapHandler == null)
			return;
		console.log("aaa");
		mapHandler.centerMarkerOverlays(vehicleInfo, true, true);

	});
}

//author：shinion
//date：2019-01-02
//获取页面上的地图控件
MyMap.getMainMapHandler2 = function()
{
	try {
		var iframeWindow = $("#testMapFrame")[0];
		var mainTab = top.jQuery("#mainTab",parent.document);
		var refresh_tab = mainTab.tabs('getSelected');
		var title = refresh_tab.panel('options').title;
		console.log("iframeWindow");
		console.log(iframeWindow);
		if (iframeWindow)
		{
			//得到地图页面上的javascript 地图对象，便于主界面调用
			var obj = iframeWindow.contentWindow.prototype.OperatorObj;
			console.log("obj");
			console.log(obj);
			return obj;
		}else{
			return null;
		}
	} catch (error) {
		console.error("获取地图控件错误" + error);
		return null;
	}
}




/**
	 *在地图上显示指定车辆的GPS数据
	 */
MyMap.showVehicleAlarmOnMap = function(vehicleId, alarmDescr)
	{
		//调用ajax方法获得车辆的实时数据
		var url = globalConfig.webPath + '/realData/refresh.action';
		var me = this;
	    $.getJSON(url, {vehicleId:vehicleId}, function(result)
		 {
			 if(result.success == false )
			 return;
			 var rd = result.data;//GPS实时数据
			 if(alarmDescr && rd.alarmStateDescr.indexOf(alarmDescr) < 0)
			 {
				 if(rd.alarmStateDescr == "无")
					 rd.alarmStateDescr = alarmDescr;
				 else
				     rd.alarmStateDescr += ","+ alarmDescr;
			 }
			 //mytree.fillGrid(rd);//填充到车辆树底部的车辆属性表格中
		     var vehicleInfo = me.convertRecord(rd);
		     var mapHandler = me.getMainMapHandler();
		     if(mapHandler == null)
			    return;
		     mapHandler.centerMarkerOverlays(vehicleInfo, true,true); 
							
		});
	}


MyMap.showVehicleListOnMap = function(gpsRealDataList,changeViewPoint,onlyUpdate)
{	
	var mapHandler = this.getMainMapHandler();
	//mapHandler.clearAllVehicle();
    mapHandler.showVehicleList(gpsRealDataList,changeViewPoint,onlyUpdate);

}
//清除地图的实时车辆地标
MyMap.clearAllVehicle = function()
{
	var mapHandler = this.getMapHandler();
	mapHandler.clearAllVehicle();
}


	//得到地图监控窗口中的地图对象
MyMap.getMapHandler = function()
	{		
		var iframeWindow = $("#mapFrame")[0];  
		var refresh_tab = $('#mainTab').tabs('getSelected');  
		var title = refresh_tab.panel('options').title; 

		if(refresh_tab) 
		{
			if( title.indexOf( "单独监控") >= 0 || title.indexOf("定时定位查车") >= 0)
			{
				iframeWindow= refresh_tab.find('iframe')[0];  
			}
		}

		if(iframeWindow)
		    return iframeWindow.contentWindow.OperatorObj; //得到地图页面上的javascript 地图对象，便于主界面调用

		return null;
	}

	MyMap.getMainMapHandler = function()
	{		
		var iframeWindow = $("#mapFrame")[0];  
		var refresh_tab = $('#mainTab').tabs('getSelected');  
		var title = refresh_tab.panel('options').title;
		if(iframeWindow)
		    return iframeWindow.contentWindow.OperatorObj; //得到地图页面上的javascript 地图对象，便于主界面调用

		return null;
	}

function getStartPOIData(keyword)
{
	if(keyword.length < 2)
		return;

	MyMap.getMapHandler().keyword = keyword;
	
	MyMap.startPOISearch.search(keyword,{forceLocal:true});
}
function getEndPOIData(keyword)
{
	if(keyword.length < 4)
		return;
	MyMap.getMapHandler().keyword = keyword;
	
	MyMap.endPOISearch.search(keyword,{forceLocal:true});
}

/**
 * 初始化地点搜索输入框的autocomplete控件，以来jquery.autocomplete.js
 */
MyMap.initAutoComplete =  function()
{
	var me =this;
	var startPOI = $("#startPOI");
	startPOI.autocomplete('url', {
	    dataCallback:getStartPOIData,
		minChars: 2,
		max: 12,
		autoFill: false,
		mustMatch: false,
		matchContains: false,
		scrollHeight: 220,
		formatItem: function(data, i, total) {
			var name = data.title + ", " + data.address;
			return name;
		}
	}).result(function(event, data, formatted) {
		me.startPoint = data;
	});
	
	var endPOI = $("#endPOI", window.parent.parent.document);
	endPOI.autocomplete('url', {
	    dataCallback:getEndPOIData,
		minChars: 0,
		max: 12,
		autoFill: false,
		mustMatch: false,
		matchContains: true,
		scrollHeight: 220,
		formatItem: function(data, i, total) {
			var name = data.title + ", " + data.address;
			return name;
		}
	}).result(function(event, data, formatted) {
		me.endPoint = data;
	});
	
	this.startPOISearch = this.getMapHandler().createLocalSearch(function(term,data)
	{
		startPOI.fillData(term,data);
	});
	this.endPOISearch = this.getMapHandler().createLocalSearch(function(term,data)
	{
		endPOI.fillData(term,data);
	});

	$("#btnPlanRoute").click(function()
	{
		me.getMapHandler().planDriveRoute(me.startPoint, me.endPoint);

	});
}



MyMap.convertRecord = function(rd)
    {
	 var realDataId = rd.ID;
	 rd.orgLatitude = parseFloat(rd.orgLatitude).toFixed(6);
	 rd.orgLongitude = parseFloat(rd.orgLongitude).toFixed(6);
	 rd.gas = parseFloat(rd.gas).toFixed(1);
	 rd.mileage = parseFloat(rd.mileage).toFixed(1);
	 //rd.velocity = parseFloat(rd.velocity).toFixed(1);
	 rd.recordVelocity = parseFloat(rd.recordVelocity).toFixed(1);
	 if(!realDataId)
		 realDataId = rd.id;
	 /**
      var vehicleInfo = {id:rd.ID, text:rd.plateNo, vehicleId:rd.vehicleId, rLat:rd.latitude,rLng:rd.longitude, tLat:rd.latitude, tLng:rd.longitude,status:rd.status,color:rd.plateColor,validate:rd.valid,direction:rd.direction,
							  angleInt:rd.direction, statusInt:0, speed:rd.velocity, warnTypeId:0, online:rd.online,
		  monitor:rd.monitor, driverName:rd.driverName, telephone:rd.telephone};
	   
	 var merged = $.extend({}, rd, vehicleInfo);*/
	 return rd;

    }
